package top.majuan.lianxi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.majuan.lianxi.base.PageBean;
import top.majuan.lianxi.base.R;
import top.majuan.lianxi.dao.TbCpuDAO;
import top.majuan.lianxi.entity.TbCpu;
import top.majuan.lianxi.service.TbCpuService;
import top.majuan.lianxi.vo.PageVo;

@Service
@RequiredArgsConstructor
@Slf4j
public class TbCpuServiceImpl implements TbCpuService {
    public final TbCpuDAO tbCpuDAO;


    @Override
    public R<String> insert(TbCpu tbCpu) {
        int i = tbCpuDAO.insert(tbCpu);
        return i == 1 ? R.ok("添加成功",null) : R.fail("添加失败",null);
    }

     @Override
     public R<String> update(TbCpu tbCpu) {
        int i = tbCpuDAO.updateById(tbCpu);
        return i == 1 ? R.ok("修改成功",null) : R.fail("修改失败",null);
    }

    @Override
    public R<TbCpu> queryByid(Integer id) {
        TbCpu tbCpu = tbCpuDAO.selectById(id);
        return tbCpu == null?R.fail("查询失败",null) : R.ok(tbCpu);

    }

    @Override
    public R<String> delete(Integer id) {
        int i = tbCpuDAO.deleteById(id);
        return i==1?R.ok("删除成功",null):R.fail("删除失败",null);
    }

    @Override
    public R<PageVo<TbCpu>> queryPage(PageBean pageBean, TbCpu tbCpu) {
        IPage<TbCpu> page = new Page<>(pageBean.getPageNumber(),pageBean.getPageSize());
        QueryWrapper<TbCpu> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbCpu.getCpu()),"cpu",tbCpu.getCpu());
        wrapper.like(StringUtils.hasText(tbCpu.getManufacturer()),"manufacturer",tbCpu.getManufacturer());
        page = tbCpuDAO.selectPage(page,wrapper);
        return R.ok(page);
    }

}
